/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hshn.mi.ajax;

import javax.enterprise.context.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.chart.ChartSeries;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.ItemSelectEvent;
import org.primefaces.model.chart.BubbleChartModel;
import org.primefaces.model.chart.BubbleChartSeries;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.MeterGaugeChartModel;
import org.primefaces.model.chart.PieChartModel;

@ManagedBean
@ApplicationScoped
public class Bean implements Serializable {

    private PieChartModel pieModel;
    private CartesianChartModel ceartesianModel;
    private BubbleChartModel bubbleModel;
    private MeterGaugeChartModel meterGaugeChartModel;

    public MeterGaugeChartModel getMeterGaugeChartModel() {
        return meterGaugeChartModel;
    }

    public BubbleChartModel getBubbleModel() {
        return bubbleModel;
    }

    public Bean() {
        createPieModel();
        creatCeartesianChartModel();
        createBubbleModel();
    }

    public PieChartModel getPieModel() {
        return pieModel;
    }

    public CartesianChartModel getCeartesianModel() {
        return ceartesianModel;
    }

    private void createPieModel() {
        pieModel = new PieChartModel();

        pieModel.set("Erhan 1", 540);
        pieModel.set("Jalal 2", 325);
        pieModel.set("Berti 3", 302);
        pieModel.set("Jim 4", 421);
        ceartesianModel = new CartesianChartModel();

    }

    private void creatCeartesianChartModel() {
        ceartesianModel = new CartesianChartModel();
        ChartSeries boys = new ChartSeries();
        boys.setLabel("Jungs");
        boys.set("2006", 55);
        boys.set("2007", 28);
        boys.set("2008", 65);
        boys.set("2009", 70);
        boys.set("2010", 120);
        boys.set("2011", 100);

        ChartSeries girls = new ChartSeries();
        girls.setLabel("Mädchen");
        girls.set("2006", 15);
        girls.set("2007", 30);
        girls.set("2008", 90);
        girls.set("2009", 70);
        girls.set("2010", 52);
        girls.set("2011", 60);

        ceartesianModel.addSeries(boys);
        ceartesianModel.addSeries(girls);
        createMeterGaugeModel();
    }

    private void createBubbleModel() {
        bubbleModel = new BubbleChartModel();

        bubbleModel.addBubble(new BubbleChartSeries("Acura", 70, 183, 55));
        bubbleModel.addBubble(new BubbleChartSeries("Alfa Romeo", 45, 92, 36));
        bubbleModel.addBubble(new BubbleChartSeries("AM General", 24, 104, 40));
        bubbleModel.addBubble(new BubbleChartSeries("Bugatti", 50, 123, 60));
        bubbleModel.addBubble(new BubbleChartSeries("BMW", 15, 89, 25));
        bubbleModel.addBubble(new BubbleChartSeries("Audi", 40, 180, 80));
        bubbleModel.addBubble(new BubbleChartSeries("Aston Martin", 70, 70, 48));
        bubbleModel.addBubble(new BubbleChartSeries("Mercedes", 10, 10, 10));

    }

    public void itemSelect(ItemSelectEvent event) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Item selected",
                "Item Index: " + event.getItemIndex() + ", Series Index:" + event.getSeriesIndex());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    private void createMeterGaugeModel() {

        List<Number> intervals = new ArrayList<Number>() {

            {
                add(20);
                add(50);
                add(120);
                add(220);
            }
        };

        meterGaugeChartModel = new MeterGaugeChartModel("km/h", 140, intervals);
    }
}
